package com.company.cloud.page.modules.mgr.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.auth.annotation.Authc;
import com.company.cloud.common.bean.BaseResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.bean.LoginUserInfo;
import com.company.cloud.common.constant.BaseConstant.EnabledEnum;
import com.company.cloud.common.constant.BaseReturnEnum;
import com.company.cloud.common.exception.BusinessException;
import com.company.cloud.common.utils.AuthContextUtils;
import com.company.cloud.page.modules.base.entity.PageTypeInfo;
import com.company.cloud.page.modules.base.service.PageTypeInfoService;
import com.company.cloud.page.modules.cache.models.PageInfoModel;
import com.company.cloud.page.modules.cache.models.PageTypeModel;
import com.company.cloud.page.modules.cache.utils.PageConvertUtils;
import com.company.cloud.page.modules.mgr.controller.req.PageTypeQuery4PageParam;
import com.company.cloud.page.modules.mgr.controller.req.PageTypeSaveParam;
import com.company.cloud.page.modules.mgr.controller.req.PageTypeUpdateParam;

import cn.hutool.core.bean.BeanUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 页面分类信息
 */
@Authc
@RestController
@RequestMapping("/mgr/type/pageConfig")
@Api(tags = "Page_Mgr_页面分类信息管理")
public class PageTypeController {
	@Autowired
	private PageTypeInfoService pageTypeInfoService;
	
	@PostMapping("/query")
	@ApiOperation(value="列表查询", response= PageInfoModel.class)
	public BaseResult query(@RequestBody PageTypeQuery4PageParam params) {
		IPage<PageTypeInfo> page = pageTypeInfoService.page(new Page<>(params.getPage(), params.getRows()),
				new QueryWrapper<PageTypeInfo>()
						.eq(StringUtils.hasText(params.getCompanyNo()), "company_no", params.getCompanyNo())
						.like(StringUtils.hasText(params.getName()),"name", params.getName())
						.eq("rows_state", EnabledEnum.ENABLED.getCode())
						.orderByDesc("cdt desc")
				);
		IPage<PageTypeModel> data=page.convert(PageConvertUtils::getPageTypeModel);
		return BaseResult.ok(data);
	}

	@PostMapping("/queryAll")
	@ApiOperation(value="列表查询(下拉框)", response= PageInfoModel.class)
	public BaseResult queryAll(@RequestBody PageTypeQuery4PageParam params) {
		LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
		List<PageTypeInfo> list = pageTypeInfoService.list(new QueryWrapper<PageTypeInfo>()
				.eq(StringUtils.hasText(params.getCompanyNo()),"company_no", params.getCompanyNo())
				.like(StringUtils.hasText(params.getName()),"name", params.getName())
				.eq("rows_state", EnabledEnum.ENABLED.getCode())
				.orderByDesc("cdt desc")
		);
		List<PageTypeModel> result=new ArrayList<PageTypeModel>();
		if(!CollectionUtils.isEmpty(list)) {
			list.forEach(item->{
				result.add(PageConvertUtils.getPageTypeModel(item));
			});
		}
		return BaseResult.ok(result);
	}

	@GetMapping("/get")
	@ApiOperation(value="查询")
	public BaseResult get(@RequestParam("id") Integer id) {
		PageTypeInfo entity = pageTypeInfoService.getById(id);
		if(entity==null || EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		return BaseResult.ok(PageConvertUtils.getPageTypeModel(entity));
	}


	@PostMapping("/save")
	@ApiOperation(value="新增")
	public BaseResult save(@RequestBody PageTypeSaveParam params) {
		ValidatorUtils.validate(params);
		PageTypeInfo oldEntity = pageTypeInfoService.getOne(new QueryWrapper<PageTypeInfo>()
				.eq("company_no", params.getCompanyNo())
				.eq("name", params.getName())
				.eq("rows_state", EnabledEnum.ENABLED.getCode())
		);
		if(oldEntity != null) {
			throw new BusinessException(BaseReturnEnum.DATA_EXISTS);
		}
		LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
		PageTypeInfo entity = new PageTypeInfo();
		BeanUtil.copyProperties(params, entity);
		entity.setRowsState(EnabledEnum.ENABLED.getCode());
		entity.addCreateParam(userInfo);
		entity.addEditParam(userInfo);
		pageTypeInfoService.save(entity);
		return BaseResult.ok();
	}
	
	@PostMapping("/update")
	@ApiOperation(value="修改")
	public BaseResult update(@RequestBody PageTypeUpdateParam params) {
		ValidatorUtils.validate(params);
		LoginSimpleUserInfo userInfo = AuthContextUtils.getUser();
		PageTypeInfo entity = pageTypeInfoService.getById(params.getId());
		if(entity==null || EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		BeanUtil.copyProperties(params, entity);
		entity.addEditParam(userInfo);
		pageTypeInfoService.updateById(entity);
		return BaseResult.ok();
	}
	
	@GetMapping("/delete")
	@ApiOperation(value="删除")
	public BaseResult delete(@RequestParam("id")Integer id) {
		PageTypeInfo entity = pageTypeInfoService.getById(id);
		if(entity==null || EnabledEnum.DISABLED.getCode().equals(entity.getRowsState())) {
			throw new BusinessException(BaseReturnEnum.DATA_NOT_EXISTS);
		}
		LoginSimpleUserInfo userInfo= AuthContextUtils.getUser();
		entity.setRowsState(EnabledEnum.DISABLED.getCode());
		entity.addEditParam(userInfo);
		pageTypeInfoService.updateById(entity);
		return BaseResult.ok();
	}
	
}

